package com.eshopkeeper.core.util;

//import com.alibaba.dubbo.config.spring.ReferenceBean;

import com.weibo.api.motan.config.BasicRefererInterfaceConfig;
import com.weibo.api.motan.config.springsupport.RefererConfigBean;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author chenshufeng
 * @version 2016年5月27日 下午4:23:06
 */
public class ReferenceUtil {

    private static final Logger logger = LogManager.getLogger(ReferenceUtil.class);

    private ReferenceUtil() {
    }

//    private static final ConcurrentMap<String, ReferenceBean<?>> referenceConfigs = new ConcurrentHashMap<String, ReferenceBean<?>>();
//
//    /** 获取Dubbo服务 */
//    public static Object refer(ApplicationContext applicationContext, String interfaceName) {
//        String key = "/" + interfaceName + ":";
//        ReferenceBean<?> referenceConfig = referenceConfigs.get(key);
//        if (referenceConfig == null) {
//            referenceConfig = new ReferenceBean<Object>();
//            referenceConfig.setInterface(interfaceName);
//            if (applicationContext != null) {
//                referenceConfig.setApplicationContext(applicationContext);
//                try {
//                    referenceConfig.afterPropertiesSet();
//                } catch (RuntimeException e) {
//                    throw (RuntimeException)e;
//                } catch (Exception e) {
//                    throw new IllegalStateException(e.getMessage(), e);
//                }
//            }
//            referenceConfigs.putIfAbsent(key, referenceConfig);
//            referenceConfig = referenceConfigs.get(key);
//        }
//        return referenceConfig.get();
//    }
    private static final ConcurrentMap<String, RefererConfigBean> referenceConfigs = new ConcurrentHashMap<String, RefererConfigBean>();

    /** 获取motan服务 */
    public static Object refer(ApplicationContext applicationContext,String interfaceName,String groupName) {
         String key = "/" + interfaceName + ":";
        RefererConfigBean referenceConfig = referenceConfigs.get(key);
        if (referenceConfig == null) {
            referenceConfig = new RefererConfigBean<Object>();
            try {
                referenceConfig.setInterface(Class.forName(interfaceName));
                BasicRefererInterfaceConfig basicReferer = new BasicRefererInterfaceConfig();
                basicReferer.setGroup(groupName);
                referenceConfig.setBasicReferer(basicReferer);
            } catch (ClassNotFoundException e) {
                logger.info("获取motan服务，setInterface错误：",e);
            }
            if (applicationContext != null) {
//                referenceConfig.setApplicationContext(applicationContext);
                referenceConfig.setBeanFactory(applicationContext);
                try {
                    referenceConfig.afterPropertiesSet();
                } catch (RuntimeException e) {
                    throw (RuntimeException)e;
                } catch (Exception e) {
                    throw new IllegalStateException(e.getMessage(), e);
                }
            }
            referenceConfigs.putIfAbsent(key, referenceConfig);
            referenceConfig = referenceConfigs.get(key);
        }
        Object object = null;
        try {
            object = referenceConfig.getObject();
        } catch (Exception e) {
            logger.info("获取motan服务，getObject错误：",e);
        }
        return object;
    }
}
